﻿#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MCMLXVII.Client.Common.Session;
using MCMLXVII.BackEnd.Services.SystemConfiguration.Messages;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.BackEnd.Services.OrganizationChart.Interfaces;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;

#endregion

namespace MCMLXVII.Tools.OrganizationChart
{
    partial class frmUserSecurity : Form
    {
        private int _MemberID;
        private string _MemberDescription;
        private int _UpdateSequence;

        private msgPermissionInfo[] _PermissionsInherited;
        private msgPermissionInfo[] _PermissionsAssigned;
        private msgPermissionInfo[] _PermissionsList;

        public int UpdateSequence
        {
            get { return _UpdateSequence; }
        }
        public frmUserSecurity( string MemberDescription, int MemberID, int updateSequence )
        {
            _MemberID  = MemberID;
            _MemberDescription  = MemberDescription ;
            _UpdateSequence = updateSequence;

            InitializeComponent();

        }

        private void frmSecurity_Load(object sender, EventArgs e)
        {

            txtFolderName.Text = _MemberID.ToString() + " - " + _MemberDescription;
            _PermissionsList = BusinessActions.GetPermissionsList();
            _PermissionsAssigned = BusinessActions.GetMemberPermissionsList(_MemberID);
            _PermissionsInherited = BusinessActions.GetMemberInheritedPermissionsList(_MemberID);

            if (_PermissionsInherited != null)
            {
                foreach (msgPermissionInfo p in _PermissionsInherited)
                {
                    gridInherited.Rows.Add(p.Name, true);
                }
            }

            foreach (msgPermissionInfo p2 in _PermissionsList )
            {
                bool assinged = false;
                if (_PermissionsAssigned != null)
                {
                    foreach (msgPermissionInfo p3 in _PermissionsAssigned)
                    {
                        if (p3.ID == p2.ID)
                        {
                            assinged = true;
                            break;
                        }
                    }
                }
                gridPermissions.Rows.Add(p2.ID, p2.Name, assinged);
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int cant = 0;
            for (int i = 0; i < gridPermissions.Rows.Count; i++)
            {
                bool b = (bool)(((DataGridViewCheckBoxCell)gridPermissions.Rows[i].Cells[2]).Value);
                if (b)
                    cant++;
            }
            int[] dat = new int[cant];
            cant = 0;
            for (int i = 0; i < gridPermissions.Rows.Count; i++)
            {
                bool b = (bool)(((DataGridViewCheckBoxCell)gridPermissions.Rows[i].Cells[2]).Value);
                if (b)
                {
                    int p = (int)((DataGridViewTextBoxCell)gridPermissions.Rows[i].Cells[0]).Value;
                    dat[cant] = p;
                    cant++;
                }
            }
            _UpdateSequence = BusinessActions.UpdateMemberPermissions(_MemberID, _UpdateSequence, dat);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void gridPermissions_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.ColumnIndex == 2) && (e.RowIndex >= 0))
            {
                ((DataGridViewCheckBoxCell)gridPermissions.Rows[e.RowIndex].Cells[e.ColumnIndex]).Value = (!(bool)((DataGridViewCheckBoxCell)gridPermissions.Rows[e.RowIndex].Cells[e.ColumnIndex]).Value);
            }
        }

    }
}